package com.fwtai.datasource;

import com.fwtai.config.DataSourceContextHolder;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Order(1)
public class DataSourceAop{

  @Before("execution(* com.fwtai..*.*Dao.find*(..))" +
    "|| execution(* com.fwtai..*.*Dao.list*(..))" +
    "|| execution(* com.fwtai..*.*Dao.query*(..))" +
    "|| execution(* com.fwtai..*.*Dao.get*(..))" +
    "|| execution(* com.fwtai..*.*Dao.select*(..))")
  public void setReadDataSourceType(){
    DataSourceContextHolder.read();
  }

  @Before("execution(* com.fwtai..*.*Dao.execute*(..))" +
    "|| execution(* com.fwtai..*.*Dao.save*(..))" +
    "|| execution(* com.fwtai..*.*Dao.add*(..))" +
    "|| execution(* com.fwtai..*.*Dao.edit*(..))" +
    "|| execution(* com.fwtai..*.*Dao.update*(..))" +
    "|| execution(* com.fwtai..*.*Dao.del*(..))")
  public void setWriteDataSourceType() {
    DataSourceContextHolder.write();
  }

  @After("execution(* com.fwtai..*.*Dao.*(..))")
  public void clearDataSource(){
    DataSourceContextHolder.remove();
  }
}